// pages/personal_center/tea_insertEveleave/tea_insertEveleave.js
import request from "../../../utils/request";
var dateTimePicker = require('../../../utils/dateTimePicker.js');


Page({
  data: {
    //时间滚动选择器
    isPickerRender: false, //开启时间选择器
    isPickerShow: false, //显示时间选择器
    pickerConfig: {
      endDate: true,
      column: "minute", //分
      dateLimit: true,
      initStartTime: "",
      initEndTime: "",
      limitStartTime: "",
      limitEndTime: "",
      isTeaStu: 1//0学生1老师
    },
    //表单滚动选择器
    showTopTips: false, //错误提示

    leaType: [],
    leaTypeIndex: 0,

    isAgree: false
  },
  async onLoad() {
    /* 取出登录信息 字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换
    let UserInfo = JSON.parse(userInfo)
    this.setData({
      /* 转完之后 存值当前页面  */
      Idnumber: UserInfo[4],
      userInfo: UserInfo
    })


  },

  bindLeaTypeChange: function (e) {
    console.log('请假类型发生选择改变，携带值为', e.detail.value);

    this.setData({
      leaTypeIndex: e.detail.value
    })
  },


  //时间滚动选择器显示
  pickerShow: function () {
    this.setData({
      isPickerShow: true,
      isPickerRender: true,
      chartHide: true
    });
  },
  //时间滚动选择器隐藏
  pickerHide: function () {
    this.setData({
      isPickerShow: false,
      chartHide: false
    });
  },

  //时间滚动选择器更改之后的时间
  setPickerTime: function (val) {
    // console.log(val);
    let data = val.detail;
    //去掉最后的秒
    // data.startTime = data.startTime.slice(0, -3);
    // data.endTime = data.endTime.slice(0, -3);
    this.setData({
      startTime: data.startTime,
      endTime: data.endTime
    });
  },

  //请假注意事项弹窗
  tapHelp: function (e) {
    if (e.target.id == 'leave') {
      this.hideHelp();
    }
  },
  showHelp: function (e) {
    this.setData({
      'leave_note': true
    });
  },
  hideHelp: function (e) {
    this.setData({
      'leave_note': false
    });
  },

  //请假注意事项是否阅读
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },

  //表单提交
  formSubmit: async function (e) {
    let startDate = e.detail.value.startDate; //开始时间
    let endDate = e.detail.value.endDate; //结束时间
    let eveMoney = e.detail.value.eveMoney; //出差费用
    let teacherName = e.detail.value.name; //教师姓名
    let eveAddress = e.detail.value.eveAddress; //出差地址
    let leaCause = e.detail.value.leaCause; //请假事由
    let isAgree = e.detail.value.isAgree //是否阅读请假注意事项
    let teacherID = 0; //老师id
   
    //登录类型为0【老师登录】，1【学生登录】
    if (this.data.userInfo[1] == 0) {
      teacherID = this.data.userInfo[0]
    } else {
      wx.showToast({
        title: '学生不能在当前页面请假',
        icon: 'none'
      })
      return;
    }

    if (teacherName == null || teacherName == "") {
      wx.showToast({
        title: '页面数据加载异常，请刷新重试',
        icon: 'none'
      })
      return;
    }


    if (eveMoney == null || eveMoney == "") {
      wx.showToast({
        title: '请填写出差费用',
        icon: 'none'
      })
      return;
    }

    if (eveAddress == null || eveAddress == "") {
      wx.showToast({
        title: '请填写出差所在地',
        icon: 'none'
      })
      return;
    }

    if ((startDate == null || startDate == "") && (endDate == null || endDate == "")) {
      wx.showToast({
        title: '请选择出差时间',
        icon: 'none'
      })
      return;
    }
   
    if (leaCause == null || leaCause == "") {
      wx.showToast({
        title: '请填写出差原因',
        icon: 'none'
      })
      return;
    }

    if (isAgree[0] != "agree") {
      wx.showToast({
        title: '请阅读出差注意事项',
        icon: 'none'
      })
      return;
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value);

    let Data = {
      teacherName:teacherName,
      eveDate: startDate,
      endTime: endDate,
      eveReason: leaCause,
      eveMoney:eveMoney,
      eveAddress:eveAddress,
      teacherID: teacherID,
    }
    console.log('Data：', Data);

    //新增请求
    let formData = await request('/WxInterface/InsertTeaLeaveEve', Data);
    console.log(formData);
    
    if(formData.State){
      wx.reLaunch({
        url: '/pages/personal_center/tea_leavedata/tea_leavedata'
      })
      wx.showToast({
        title: formData.Text,
        icon:'',
        duration:1800
      });
      
    }else{
      wx.showToast({
        title: formData.Text,
        icon:'none',
        duration:1800
      })
    }
   }

})